import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*; 
public class AnalyzeDir4Mr {

	static Properties dir_props= new Properties();
	static PrintWriter out;
	static PrintWriter out2;
	static PrintWriter outrm;
	public static void main(String[] args) throws Exception{
		dir_props=loadProperties(".dir.txt");
		outrm=new PrintWriter("dirs_out4_rm.txt");
		
		List<String> coveredDirs=new ArrayList<String>();
		BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("dirs1.log")));
		Set<String> _del=new TreeSet<String>();
		String _line="";
		if(args.length>0 && args[0] != null && args[0].equals("d1")){
			while((_line=br.readLine())!= null){
				if(_line.startsWith("meld")){
					coveredDirs.add(_line);
					
				}
			}
		}
		br.close();
		
		out=new PrintWriter("dirs_out4.txt");
		out2=new PrintWriter("dirs4.log");

		File _dir= new File("tmp2");
		File[] farr=_dir.listFiles();
		Long mx=Long.MAX_VALUE;
		TreeMap<Long,List<String>> fmap= new TreeMap<Long, List<String>>();
		TreeMap<String,Long> szMap= new TreeMap<String, Long>();
		for(File _f:farr){
			
			String _name_=_f.getName();
			int _ii=_name_.indexOf("_");
			Long sz=mx-Long.parseLong(_name_.substring(0,_ii));
			szMap.put(_name_, sz);
			Long __sz=_f.length();
			if(__sz==0)
				continue;
				

			List<String> strs= fmap.get(__sz);
			if(strs==null)
				strs=new ArrayList<String>();
			strs.add(_f.getName());
			fmap.put(__sz, strs);
		}
		Set<Long> keys=fmap.keySet();
		long gain=0;
		for(Long key:keys){
			if(fmap.get(key).size()>1){
				List<String> vals=fmap.get(key);
				out2.println("====++"+vals.size()+".."+(mx-key));
				out.println("=======SIZE="+"=====leng="+(mx-key)+"============");
				List<String> newvals=new ArrayList<String>();
				newvals.addAll(vals);
				for(String val:vals){
					
					String _val_path=path(val.substring(15));
					int __v=_val_path.lastIndexOf("/");
					List<String> _m=new ArrayList<String>();
					_m.add(_val_path);

					for(int _i=0;_i<newvals.size();_i++){
						String newval=newvals.get(_i);
						if(!newval.equals(val)){
							String _new_val_path=path(newval.substring(15));

							
							int __n=_new_val_path.lastIndexOf("/");
							String _pn1=_new_val_path.substring(0,__n);
							String _pv1=_val_path.substring(0,__v);
							String _par1="meld \""+_pn1+"\" \""+_pv1+"\"";
							String _par2="meld \""+_pv1+"\" \""+_pn1+"\"";
							String _par3="meld \""+_new_val_path+"\" \""+_val_path+"\"";
							String _par4="meld \""+_val_path+"\" \""+_new_val_path+"\"";
							if(_del.contains(_pn1)){
								_del.add(_new_val_path);
							}
							if(_del.contains(_pv1)){
								_del.add(_val_path);
							}
							if(coveredDirs.contains(_pn1)){
								coveredDirs.add(_new_val_path);
							}
							if(coveredDirs.contains(_pv1)){
								coveredDirs.add(_val_path);
							}
							if(coveredDirs.contains(_new_val_path)&& coveredDirs.contains(_val_path))
								continue;

							
							if(dif.comp(new String[]{"tmp2/"+val,"tmp2/"+newval})){
								Long _sz1=(999999999999999L-Long.parseLong(val.substring(0,15)));
								Long _sz2=(999999999999999L-Long.parseLong(newval.substring(0,15)));
								_m.add(_new_val_path);
								
									if(_sz1>=_sz2){
										
										 if(!coveredDirs.contains(_new_val_path) && !_del.contains(_val_path)){
											out.println("meld \""+_val_path+"\" \""+_new_val_path+"\"");
										outrm.println("meld \""+_val_path+"\" \""+_new_val_path+"\"");
										outrm.println("rm -rf \""+_new_val_path+"\"");
										coveredDirs.add(_val_path);
										coveredDirs.add(_new_val_path);
										_del.add(_new_val_path);
										gain+=_sz2;
										}
									}else if(!coveredDirs.contains(_val_path)  && !_del.contains(_new_val_path)){
										out.println("meld \""+_new_val_path+"\" \""+_val_path+"\"");
										outrm.println("meld \""+_new_val_path+"\" \""+_val_path+"\"");
										outrm.println("rm -rf \""+_val_path+"\"");
										coveredDirs.add(_val_path);
										coveredDirs.add(_new_val_path);
										_del.add(_val_path);
										gain+=_sz1;
									}

							}
							else{
							}
						}else{
						}
					}

					
				}
			}
		}
		out.println("\ngain="+gain);
		out.close();
		outrm.close();
		out2.close();
		
		
		
		
	}
	

	static private String path(String file){
		int i=file.length();
		int _jj=file.lastIndexOf("_");
		return (String)dir_props.get(file.substring(1,_jj));
		
	}
	


	public static Properties loadProperties(String fileName) throws Exception{
		ByteArrayOutputStream bao=new ByteArrayOutputStream();
		FileInputStream fis=new FileInputStream(fileName);
		Crypto.getInstance().decrypt(fis, bao);
		byte[] barr=bao.toByteArray();
		ByteArrayInputStream bis= new ByteArrayInputStream(barr);
		Properties props= new Properties();
		props.loadFromXML(bis);
		bao.close();
		fis.close();
		return props;
	}
	public static String encrypt(String str){
		return Crypto.getInstance().encrypt(str);
	}
	public static String decrypt(String str){
		return Crypto.getInstance().decrypt(str);
	}


}

